﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FractionNumber
{
    class Fraction : IFormattable
    {
        private int nominator = 0;
        private int denominator = 1;

        private static int GreatestCommonDevisor(int a, int b)
        {
            if (b == 0)
            {
                return a;
            }
            else
            {
                return GreatestCommonDevisor(b, a % b);
            }
        }

        public Fraction(int nominator, int denominator)
        {
            int greatestCommonDevisor = GreatestCommonDevisor(nominator, denominator);
            this.nominator = nominator / greatestCommonDevisor;
            this.denominator = denominator / greatestCommonDevisor;
        }

        public string ToString(string format, IFormatProvider formatProvider)
        {
            if (format == "N")
            {
                return String.Format("{0}/{1}", this.nominator, this.denominator);
            }
            else if (format == "R")
            {
                return String.Format("{0:F2}", (double)this.nominator / this.denominator);
            }
            else if (format == null)
            {
                return String.Format("{0}", (double)this.nominator / this.denominator);
            }
            else
            {
                throw new FormatException(String.Format("Invalid format: {0}", format));
            }
        }
    }
}
